/**
 * Created with IntelliJ IDEA.
 * Description:
 * https://gitee.com/li--jiaqiang/java-test.git
 * git pull
 * git pull origin master
 * git pull origin master --allow-unrelated-histories
 * User: 86187
 * Date: 2022-05-10
 * Time: 23:39
 */

//Scanner scan = new Scanner(System.in);

import java.util.Scanner;

public class TestDemo {
    public static int getNext(int n) {
        int totalSum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
    }

    public static boolean isHappy(int n) {
        int slowRunner = n;//2
        int fastRunner = getNext(n);//4
        while (fastRunner != 1 && slowRunner != fastRunner) {
            slowRunner = getNext(slowRunner);//2 4
            fastRunner = getNext(getNext(fastRunner));//16 37 58
        }
        return fastRunner == 1;
    }

    public static void main(String[] args) {
        System.out.println(isHappy(2));
    }
}
